﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcContrib.Pagination;

using ENail.Domain.Core;
using Equation.Enterprise.Framework.Domain.Model;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;
using ENail.Domain.Core.Contract.Services;


namespace ENail.Portal.Web.Models
{
    public class NailServiceViewModel: 
        BaseViewModel<Int16>
    {
        [Required(ErrorMessage="Service name is empty.")]
        public string ServiceName { get; set; }

        public short ParentServiceId { get; set; }
        public short CategoryId { get; set; }

        [Required(ErrorMessage="Price is empty.")]
        //[RegularExpression(@"\d+\.?\d+", ErrorMessage="Price is not number")]
        public double Price { get; set; }

        public string Description { get; set; }

        public IEnumerable<NailService> ServiceList { get; set; }
        public IEnumerable<Category> CategoryList { get; set; }
    }

    public class NailServiceListViewItem : BaseViewModel<Int16>
    {
        public string ServiceName { get; set; }
        public short ParentServiceId { get; set; }
        public string ParentServiceName { get; set; }
        public short CategoryId { get; set; }
        public string CategoryName { get; set; }
        public double Price { get; set; }
        public bool IsSelected { get; set; }
    }

    public class NailServiceViewModelMapper: ViewModelMapper<NailServiceViewModel, NailService>
    {
        private INailServiceService _nailService;
        private ICategoryService _categoryService;

        public NailServiceViewModelMapper(INailServiceService nailSerivce,
                                          ICategoryService categoryService)
        {
            _nailService = nailSerivce;
            _categoryService = categoryService;
        }

        public override void MapDomainToView(NailService domainModel, NailServiceViewModel viewModel)
        {
 	        Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.Id = domainModel.Id;
            viewModel.ServiceName = domainModel.ServiceName;
            viewModel.ParentServiceId = domainModel.ParentService == null ? (short)0 : domainModel.ParentService.Id;
            viewModel.CategoryId = domainModel.Category == null ? (short)0 : domainModel.Category.Id;
            viewModel.Price = domainModel.Price;
            viewModel.Description = domainModel.Description;

            viewModel.ServiceList = _nailService.GetAllActiveNailServices();
            viewModel.CategoryList = _categoryService.GetAllActiveCategories();
        }

        public override void MapViewToDomain(NailServiceViewModel viewModel, NailService domainModel)
        {
            Check.Require(viewModel != null);
            Check.Require(domainModel != null);

            domainModel.ServiceName = viewModel.ServiceName;
            domainModel.ParentService = viewModel.ParentServiceId == 0 ? new NailService() : _nailService.GetNailService(viewModel.ParentServiceId);
            domainModel.Category = viewModel.CategoryId == 0 ? new Category() : _categoryService.GetCategory(viewModel.CategoryId);
            domainModel.Price = viewModel.Price;
            domainModel.Description = viewModel.Description;	        
        }
    }

    public class NailServiceListViewModelMapper : ListViewModelMapper<NailServiceListViewItem, NailService>
    {
        public const int DEFAULT_PAGE = 1;
        public const int PAGE_SIZE = 3;

        public override void MapDomainToView(List<NailService> domainModel, GenericListViewModel<NailServiceListViewItem> viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.List = (from e in domainModel
                              where e.IsActive && (viewModel.Filter == null || viewModel.Filter.Trim() == "" || e.ServiceName.ToLower().Contains(viewModel.Filter.Trim().ToLower()))
                              select new NailServiceListViewItem
                              {
                                  Id = e.Id,
                                  ServiceName = e.ServiceName,
                                  ParentServiceId = e.ParentService == null ? (short)0 : e.ParentService.Id,
                                  ParentServiceName = e.ParentService == null ? null : e.ParentService.ServiceName,
                                  CategoryId = e.Category == null ? (short)0 : e.Category.Id,
                                  CategoryName = e.Category == null ? null : e.Category.CategoryName,
                                  Price = e.Price,
                                  IsSelected = false
                              })
                              .OrderBy(x => x.CategoryName)
                              .AsPagination(viewModel.Page ?? DEFAULT_PAGE, PAGE_SIZE);
        }

        public override void MapViewToDomain(GenericListViewModel<NailServiceListViewItem> viewModel, List<NailService> domainModel)
        {
            throw new NotImplementedException();
        }
    }
}